package cn.edu.zzuli.wechat.user.service.impl;

import cn.edu.zzuli.model.entity.ComplaintInfo;
import cn.edu.zzuli.wechat.user.mapper.ComplaintMapper;
import cn.edu.zzuli.wechat.user.service.ComplaintService;
import cn.edu.zzuli.common.utils.ConstantOss;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, ComplaintInfo> implements ComplaintService {


    @Override
    public Boolean apply(String[] urls, String description, Integer userId, String type) {
        ComplaintInfo complaintInfo = new ComplaintInfo();

        System.out.println(userId);
        complaintInfo.setUserId(userId);
        complaintInfo.setType(type);
        complaintInfo.setDescription(description);

        if (urls != null) {

            complaintInfo.setUrl(urls[0]);

            if (urls.length > 1) {
                complaintInfo.setUrl2(urls[1]);
            }

            if (urls.length > 2) {
                complaintInfo.setUrl3(urls[2]);
            }
        }
        baseMapper.insert(complaintInfo);
        return true;
    }

    @Override
    public String upload(MultipartFile file) {
        String endpoint = ConstantOss.ENDPOINT;
        String accessKeyId = ConstantOss.ACCESS_KEY_ID;
        String accessKeySecret = ConstantOss.SECRET;
        String bucket = ConstantOss.BUCKET;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
        InputStream inputStream = null;
        try {
            String fileName = file.getOriginalFilename();
            String uuid = "" + new Random().nextInt(1000);
            //uuid = UUID.randomUUID().toString().replace("-","");
            fileName = uuid + fileName;

            //在线预览
            ObjectMetadata objectMetadata= new ObjectMetadata();
            objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf("."))));

            inputStream = file.getInputStream();
            ossClient.putObject(bucket, fileName, inputStream,objectMetadata);

            //获取路径
            String url = "https://" + bucket + "." + endpoint + "/" + fileName;
            System.out.println(url);
            return url;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }finally {
            // 关闭OSSClient。
            ossClient.shutdown();
        }
    }

    public static String getcontentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpg";
        }
        if (FilenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase(".pptx") ||
                FilenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase(".docx") ||
                FilenameExtension.equalsIgnoreCase(".doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "image/jpg";
    }

}
